var ctx;
var userAccount = "";

function setCtx(ctx){
	this.ctx = ctx;
}


function ifNull(mess){
	if(mess == null || mess == undefined){
		mess = "";
	}
	
	return mess;
}

//验证广告位类型
function checkAdCategoryType(){
	var adCategoryid = $("#adCategoryid").val();
	if(adCategoryid == null || adCategoryid == ""){
		layer.tips('请选择广告位类型!', '#adCategoryid', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}
//验证标题
function checkTitle(){
	var title = $("#title").val();
	//解决ie9以下placeholder与value重复的问题
	if(title == $("title").attr("placeholder")){
		title = "";
	}
	if(title == null || title == ""){
		layer.tips('标题不能为空!', '#title', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证开始时间
function checkBeginTime(){
	var beginTime = $("#beginTime").val();
	//解决ie9以下placeholder与value重复的问题
	if(beginTime == $("beginTime").attr("placeholder")){
		beginTime = "";
	}
	if(beginTime == null || beginTime == ""){
		layer.tips('请选择开始时间!', '#beginTime', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证结束时间
function checkEndTime(){
	var endTime = $("#endTime").val();
	//解决ie9以下placeholder与value重复的问题
	if(endTime == $("endTime").attr("placeholder")){
		endTime = "";
	}
	if(endTime == null || endTime == ""){
		layer.tips('请选择结束时间!', '#endTime', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}
//验证对象类型
function checkObjType(){
	var objType = $("#objType").val();
	if(objType == null || objType == ""){
		layer.tips('请选择对象类型!', '#objType', {
		    tips: 3,
		    time: 1000
		});
		return false;
	}else{
		return true;
	}
}

//验证商品
function checkGoods(){
	var goodsId = $("#goodsId").val();
	if(goodsId == null || goodsId == ""){
		layer.tips('请选择商品!', '#goodsId', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证网页地址
function checkURL(){
	var webUrl = $("#webUrl").val();
	if(webUrl == null || webUrl == ""){
		layer.tips('请填写网页地址!', '#webUrl', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//保存或新增方法
var objId;
function saveAdInfo() {
	var adInfoId = $("#adInfoId").val();
	var title = $("#title").val();
	var adCategoryid = $("#adCategoryid").val();
	var beginTime = $("#beginTime").val();
	var endTime = $("#endTime").val();
	var objType = $("#objType").val();
	if($("#objType option:selected").val()==1){
		objId = $("#shopId").val();
	}
	if($("#objType option:selected").val()==2){
		objId = $("#goodsId").val();
	}
	if($("#objType option:selected").val()==3){
		objId = $("#shopId").val();
	}
	var webUrl = $("#webUrl").val();
	var remark = $("#remark").val();
	var imageLogo = $('#imageLogo').val();
    if(checkAdCategoryType()&&checkTitle()&&checkBeginTime()&&checkEndTime()&&checkObjType()){
				$.ajax({
					type : "POST",
					url : ctx + "/ad/saveOrUpdateAdInfo",
					cache : false,
					dataType : 'json',
					data: {
						title:title,
						id:adInfoId,
						adCategoryId:adCategoryid,
						beginTime:beginTime,
						endTime:endTime,
						objType:objType,
						objId:objId,
						webUrl:webUrl,
						remark:remark,
						img : imageLogo
					},
					success : function(result) {
						if (result.success) {
							layer.msg("操作成功",{
								icon: 1,
								time: 2000 //2秒关闭（如果不配置，默认是3秒）
							},function(){
								loadContent("/ad/adApply");
							});
						} else {
							layer.msg("操作失败",{
								icon: 2
							});
						}
					}
				});
    }
}

//返回上一页
function cancel(){
	loadContent("/ad/adApply"); 
}

function approved(id, state) {
	var message;
	switch (state) {
	case 1:
		message = "是否拒绝通过？";
		break;
	case 2:
		message = "是否禁用？";
		break;
	case 3:
		message = "是否启用？";
		break;
	case 4:
		message = "是否通过？";
		break;
	default:
		break;
	}
	layer.confirm(message, {
		icon : 3,
		title : '提示'
	}, function(index) {
		$.ajax({
			type : "POST",
			url : ctx + "/ad/adApplyUpdate",
			cache : false,
			dataType : 'json',
			data : {
				id : id,
				state : state
			},
			success : function(result) {
				if (result.success) {
					layer.msg("操作成功", {
						icon : 1,
						time : 1000
					// 2秒关闭（如果不配置，默认是3秒）
					}, function() {
						if (state == 1) {
							loadContent("/ad/adApply");
						} else {
							loadContent("/ad/adApply");
						}

					});
				} else {
					layer.msg("操作失败", {
						icon : 2
					});
				}
			}
		});
		layer.close(index);
	});
}


